﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Growatt.CreateCCode
{
    public class HelperImport
    {

        /// <summary>
        /// 将DataTable类型的数据转换成List<T>集合 T实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static List<T> DataTableToList<T>(DataTable dataTable)
        {
            try
            {
                List<T> list = new List<T>();
                if (dataTable != null)
                {
                    Type targetType = typeof(T);
                    PropertyInfo[] allPropertyArray = targetType.GetProperties();
                    foreach (DataRow rowElement in dataTable.Rows)
                    {
                        T element = Activator.CreateInstance<T>();
                        foreach (DataColumn columnElement in dataTable.Columns)
                        {
                            foreach (PropertyInfo property in allPropertyArray)
                            {
                                if (property.Name.Equals(columnElement.ColumnName))
                                {
                                    if (rowElement[columnElement.ColumnName] == DBNull.Value)
                                    {
                                        property.SetValue(element, null, null);
                                    }
                                    else
                                    {
                                        //property.SetValue(element, rowElement
                                        //[columnElement.ColumnName], null);
                                        string readvalue = rowElement[columnElement.ColumnName].ToString();
                                        if (readvalue != null)
                                        {
                                            //属性类型
                                            string typename = property.PropertyType.Name;
                                            switch (typename)
                                            {
                                                case "Boolean":
                                                    var val = readvalue;
                                                    if (string.IsNullOrEmpty(readvalue) || val == "0") { property.SetValue(element, false); }
                                                    else if (val == "1")
                                                        property.SetValue(element, true);
                                                    break;

                                                case "Int64":
                                                case "Int32":
                                                    if (int.TryParse(readvalue, out int reInt))
                                                    {
                                                        property.SetValue(element, reInt);
                                                    }
                                                    else property.SetValue(element, default(int));
                                                    break;

                                                case "Int16":
                                                    if (short.TryParse(readvalue, out short reshort))
                                                    {
                                                        property.SetValue(element, reshort);
                                                    }
                                                    else property.SetValue(element, default(short));
                                                    break;

                                                case "DateTime":
                                                    if (DateTime.TryParse(readvalue, out DateTime reDate))
                                                    {
                                                        property.SetValue(element, reDate);
                                                    }
                                                    else property.SetValue(element, default(DateTime));
                                                    break;

                                                case "Single":
                                                    if (Single.TryParse(readvalue, out Single reSingle))
                                                    {
                                                        property.SetValue(element, reSingle);
                                                    }
                                                    else property.SetValue(element, default(Single));
                                                    break;

                                                case "Double":
                                                    if (Double.TryParse(readvalue, out Double reDouble))
                                                    {
                                                        property.SetValue(element, reDouble);
                                                    }
                                                    else property.SetValue(element, default(Double));
                                                    break;

                                                case "String":
                                                    property.SetValue(element, readvalue);
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        list.Add(element);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
